package com.xinchen.ssh.demo.core.entity.base;

import com.xinchen.ssh.demo.utils.ObjectUtils;

import javax.persistence.*;

/**
 * Description：
 *
 * @author Xin Chen
 * @version 1.0
 * @date 2016/12/15 11:07
 * @see 2016/12/15 11:07 Created
 ****************************************/
@MappedSuperclass
public abstract class AbstractEntity implements Entity {
    private static final long serialVersionUID = -3922123931186043789L;
    @Column(
            name = "id",
            unique = true
    )
    @Id
    @GeneratedValue(
            strategy = GenerationType.AUTO
    )
    private long id;

    public AbstractEntity() {
    }

    public String toXml() {
        return ObjectUtils.toXml(this);
    }

    public String toJSON() {
        return ObjectUtils.toJSON(this);
    }

    public String toString() {
        return this.toJSON();
    }

    public Object clone() {
        return ObjectUtils.serializaClone(this);
    }

    public final int hashCode() {
        return (int)this.getId();
    }

    public final boolean equals(Object obj) {
        return obj == null?false:(this == obj?true:(obj.getClass().equals(this.getClass())?obj.hashCode() == this.hashCode():false));
    }

    public long getId() {
        return this.id;
    }

    public void setId(long id) {
        this.id = id;
    }
}
